package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.constant.SystemPermissions;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dao.entity.UserVocAISituationDO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.UserVocAISituationDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.UserVocAISituationCommand;
import com.cskaoyan.wordmemorize.service.AIReadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 顾海龙
 * @version 1.0
 */
@RestController
@ResponseBody
public class AIReadController {

    @Autowired
    AIReadService aiReadService;

    //添加ai阅读模式
    @PostMapping("/admin/voc/ai-read/situation")
    @SaAdminCheckPermission(SystemPermissions.AIRead.Situation.ADD)
    public Result addAIRead(@RequestBody UserVocAISituationCommand userVocAISituationCommand){
        try {
            aiReadService.addAISituation(userVocAISituationCommand);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

    //修改指定ai阅读模式
    @PutMapping("/admin/voc/ai-read/situation")
    @SaAdminCheckPermission(SystemPermissions.AIRead.Situation.UPDATE)
    public Result updateAIRead(@RequestBody UserVocAISituationCommand userVocAISituationCommand){
        try {
            aiReadService.updateAISituation(userVocAISituationCommand);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

    //批量删除ai阅读模式
    @DeleteMapping("/admin/voc/ai-read/situation")
    @SaAdminCheckPermission(SystemPermissions.AIRead.Situation.BATCH_DELETE)
    public Result deleteAIReadByList(@RequestBody BatchDeleteCommand batchDeleteCommand){
        try {
            List<String> idStr = batchDeleteCommand.getIds();
            List<Long> ids = idStr.stream().map(Long::parseLong).collect(Collectors.toList());
            aiReadService.batchDeleteAISituationOption(ids);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

    //获取ai阅读场景列表
    @GetMapping("/admin/voc/ai-read/situation/list")
    @SaAdminCheckPermission(SystemPermissions.AIRead.Situation.SEARCH)
    public Result getAIReadList(@ModelAttribute PageRequest pageRequest){
        PageDTO<UserVocAISituationDTO> aiSituationList = null;
        try {
            aiSituationList = aiReadService.getAISituationList(pageRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.ok(aiSituationList);
    }

    //删除指定ai阅读模式
    @DeleteMapping("/admin/voc/ai-read/situation/{id}")
    @SaAdminCheckPermission(SystemPermissions.AIRead.Situation.DELETE)
    public Result deleteAIRead(@PathVariable Long id){
        try {
            aiReadService.deleteAISituation(id);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

    //删除指定ai阅读模式的选项
    @DeleteMapping("/admin/voc/ai-read/option/{id}")
    public Result deleteAIReadItem(@PathVariable Long id){
        try {
            aiReadService.deleteAISituationOption(id);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }
}
